#include "add.h"
#include "ui_add.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QMessageBox.h"
#include "stdlib.h"
#include "QTime"
extern int rank;
QString line;

add::add(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::add)
{
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    ui->setupUi(this);

    QString rank_str = QString::number(rank);
    ui->lineEdit_3->setText(rank_str);

    QString total_str = QString::number(total+1);
    ui->lineEdit_4->setText(total_str);

    ui->lineEdit_2->setFocus();

    if(rank==1||rank==2){
        if(rank==1){
            a = (rand() % 10);
            b = (rand() % 10);
        }
        else if(rank==2){
            a = (rand() % 100);
            b = (rand() % 100);
        }
        sum = a + b;
        QString a_str = QString::number(a);
        QString b_str = QString::number(b);
        line = a_str + "+" + b_str + "=";
    }
    else{
        a = (rand() % 100);
        b = (rand() % 100);
        c = (rand() % 100);
        sum = a + b + c;
        QString a_str = QString::number(a);
        QString b_str = QString::number(b);
        QString c_str = QString::number(c);
        line = a_str + "+" + b_str + "+" + c_str + "=";
    }
    ui->lineEdit->setText(line);
}

add::~add()
{
    delete ui;
}

void add::on_pushButton_clicked()  //下一题
{
    check_sum();
    QString total_str = QString::number(total+1);
    ui->lineEdit_4->setText(total_str);

    if(rank==1||rank==2){
        if(rank==1){
            a = (rand() % 10);
            b = (rand() % 10);
        }
        else if(rank==2){
            a = (rand() % 100);
            b = (rand() % 100);
        }
        sum = a + b;
        QString a_str = QString::number(a);
        QString b_str = QString::number(b);
        line = a_str + "+" + b_str + "=";
    }
    else{
        a = (rand() % 100);
        b = (rand() % 100);
        c = (rand() % 100);
        sum = a + b + c;
        QString a_str = QString::number(a);
        QString b_str = QString::number(b);
        QString c_str = QString::number(c);
        line = a_str + "+" + b_str + "+" + c_str + "=";
    }
    ui->lineEdit->setText(line);
    ui->lineEdit_2->setFocus();
}

void add::on_pushButton_2_clicked()  //答题结果
{
    QString total_number = "答题总数:";
    QString total_str = QString::number(total);
    total_number = total_number + total_str;

    QString right_number = ",  答对个数:";
    QString right_str = QString::number(right);
    right_number = right_number + right_str;

    QString wrong_number = ",  答错个数:";
    QString wrong_str = QString::number(wrong);
    wrong_number = wrong_number + wrong_str;

    QString message_output = total_number + right_number + wrong_number;
    QMessageBox::information(NULL, "答题结果", message_output, QMessageBox::Yes);
}

void add::on_pushButton_3_clicked()  //返回
{
    emit sendsignal();
    add::close();
}


void add::check_sum()
{
    QString sum_str = QString::number(sum);
    QString message_output ="正确答案：" + sum_str;

    QString my_sum_str = ui->lineEdit_2->text();
    int my_sum = my_sum_str.toInt();
    total++;
    if(my_sum == sum){
        right++;
        //QMessageBox::warning(this, "right", tr("right!"), QMessageBox::Yes, QMessageBox::No);
    }else{
        wrong++;
        QMessageBox::information(NULL, "答错提醒", message_output, QMessageBox::Yes);
        //QMessageBox::warning(this, "warning", tr("wrong!"), QMessageBox::Yes);
        //QMessageBox::warning(this, "warning", tr("listen server failed!"), QMessageBox::Yes, QMessageBox::No);
    }
    ui->lineEdit_2->clear();
}
